<template>
  <!--教师端登录-->
  <div class="student-login">
    <div class="content">
      <div class="left">
        <p class="left-title">枫叶云直播教育平台</p>
        <img
          class="login-bg"
          src="../../assets/image/login-left-bg.png"
          alt=""
          srcset=""
        />
      </div>
      <div class="right">
        <p class="title">欢迎使用枫叶云直播</p>
        <p class="desc">学生端</p>
        <el-form ref="formRef" :model="ruleForm" :rules="rules">
          <el-form-item label="" prop="username">
            <el-input
              :size="size"
              v-model="ruleForm.username"
              placeholder="手机号"
            >
              <template #prefix>
                <img
                  class="input-icon"
                  src="../../assets/image/user.png"
                  alt=""
                  srcset=""
                />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item label="" prop="password">
            <el-input
              type="password"
              :size="size"
              style="margin-top: 10px"
              v-model="ruleForm.password"
              placeholder="密码"
              show-password
            >
              <template #prefix>
                <img
                  class="input-icon"
                  src="../../assets/image/pwd.png"
                  alt=""
                  srcset=""
                />
              </template>
            </el-input>
          </el-form-item>

          <el-form-item>
            <div class="tool">
              <div>
                <el-checkbox v-model="remember" label="记住密码" :size="size" />
              </div>

              <span @click="mode.value = modeType.forgot_pwd" class="desc-text"
                >忘记密码？</span
              >
            </div>
          </el-form-item>

          <el-form-item label="">
            <el-button
              class="login-btn"
              :size="size"
              :loading="loading"
              color="#E1463B"
              @click="handleSubmit"
            >
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { removeAccount, setAccount, getAccount } from '@/views/login/cache'
import router from '@/router'
const { username, password } = getAccount()

const userStore = useUserStore()
const formRef = ref(null)
const remember = ref(!!password)
const size = 'large'
const loading = ref(false)

const modeType = {
  login: 'login',
  forgot_pwd: 'forgot_pwd'
}
const mode = ref(modeType.login)

const ruleForm = reactive({
  username: username,
  password: password
})

const rules = {
  username: {
    required: true,
    message: '请输入用户名',
    trigger: 'change'
  },
  password: {
    required: true,
    message: '请输入密码',
    trigger: 'change'
  }
}

async function handleSubmit() {
  await formRef.value.validate()
  try {
    loading.value = true
    userStore.clearUserInfo()
    await userStore.userLogin(ruleForm)
    await userStore.studentInfo()
    loading.value = false
    if (remember.value) {
      // 记住密码
      setAccount(ruleForm)
    } else {
      removeAccount() // 取消记住密码
    }
    router.replace('/studentHome')
  } catch (e) {
    loading.value = false
    console.error(e)
  }
}
</script>

<style lang="scss" scoped>
.student-login {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background-color: #fff;
  .content {
    display: flex;
    width: 1100px;
    background: #ffffff;
    box-shadow: 0px 0px 43px 0px rgba(0, 0, 0, 0.06);
    border-radius: 30px;
    overflow: hidden;

    .left {
      height: 100%;
      background: linear-gradient(180deg, #d31515, #e1463b);
      color: #fff;
      width: 50%;
      padding: 0 40px;

      .left-title {
        font-size: 70px;
        text-align: center;
        margin-top: 80px;
        padding: 0 40px;
        margin-bottom: 60px;
      }

      .login-bg {
        width: 100%;
      }
    }

    .right {
      width: 50%;
      padding: 80px 90px 60px;

      .title {
        font-size: 40px;
        text-align: center;
        margin-bottom: 20px;
      }
      .desc {
        font-size: 30px;
        text-align: center;
        margin-bottom: 50px;
      }

      .tool {
        display: flex;
        justify-content: space-between;
        align-items: center;
        width: 100%;
      }

      .login-btn {
        width: 100%;
        margin-top: 100px;
      }

      .desc-text {
        color: #606266;
        cursor: pointer;
      }

      .input-icon {
        width: 20px;
        height: 20px;
      }
    }
  }
}
</style>
